package com.tran.app.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import com.tran.app.MyApp
import com.tran.app.data.CourseSelectionCancellation

@Dao
interface CourseSelectionCancellationDao {
    @Insert
    fun insert(cancellation: CourseSelectionCancellation)

    @Query("SELECT * FROM CourseSelectionCancellation WHERE teacher_account = :teacherAccount")
    fun getAllCancellationsByTeacherAccount(teacherAccount: String): List<CourseSelectionCancellation>

    @Delete
    fun delete(cancellation: CourseSelectionCancellation)

    @Query("DELETE FROM CourseSelectionCancellation WHERE CourseSelection_id = :courseSelectionId")
    fun deleteByCourseSelectionId(courseSelectionId: Long)

    @Query("SELECT * FROM CourseSelectionCancellation WHERE CourseSelection_id = :courseSelectionId")
    fun getCancellationByCourseSelectionId(courseSelectionId: Long): CourseSelectionCancellation?

    @Transaction
    @Query("SELECT * FROM CourseSelectionCancellation WHERE CourseSelection_id = :courseSelectionId")
    fun deleteCourseSelectionAndCancellation(courseSelectionId: Long) {
        val cancellation = getCancellationByCourseSelectionId(courseSelectionId)
        if (cancellation != null) {
            delete(cancellation)
            AppDatabase.getInstance(MyApp.instance).CourseSelectionDao().deleteByCourseSelectionId(courseSelectionId)
        }
    }
}